package com.bilibili.juc.reference;
import java.lang.ref.SoftReference;
import java.util.concurrent.TimeUnit;
/**
 * @author zuife
 * @description
 * @since 2024-05-05 20:07:24
 */
public class SoftReferenceDemo {
    public static void main(String[] args) {
        SoftReference<MyObject> softReference = new SoftReference<>(new MyObject());
        System.out.println("-----------softReference:" + softReference.get());
        System.gc();
        //暂停几秒钟线程
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("-----------gc after 内存够用 softReference:" + softReference.get());
        try {
            byte[] bytes = new byte[1024 * 1024 * 20];
         } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            System.out.println("-----------gc after 内存不够用 softReference:" + softReference.get());
        }

    }
}

